package com.ilop.permission;

import androidx.fragment.app.FragmentActivity;

import com.ilop.permission.callback.OnPermissionCallBack;

/**
 * @author skygge
 * @date 6/17/22.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：
 */
public class PermissionProxy {

    private static final String TAG = PermissionProxy.class.getSimpleName();

    private static final int PERMISSION_LOCATION = 0;

    private static final int PERMISSION_CAMERA = 1;

    private static final int PERMISSION_PUSH = 2;

    private OnPermissionCallBack mCallBack;

    private FragmentActivity mActivity;

    private static final class InstanceHolder {
        public static final PermissionProxy mInstance = new PermissionProxy();
    }

    public static PermissionProxy getInstance(){
        return InstanceHolder.mInstance;
    }

    public PermissionProxy() {
    }

    //TODO : 获取位置信息权限
    public void onCheckLocationPermission(FragmentActivity activity, OnPermissionCallBack callBack) {
        this.mCallBack = callBack;
        this.mActivity = activity;
        boolean granted = PermissionUtil.onGetLocationPermission(mActivity);
        if (granted) onCheckGPSEnable();
        else {
            PermissionDialog dialog = new PermissionDialog(mActivity, PERMISSION_LOCATION, mCallBack);
            dialog.show(mActivity.getSupportFragmentManager(), TAG);
        }
    }

    //TODO : 检查位置权限是否打开
    public void onCheckLocationEnable(boolean granted) {
        if (granted) onCheckGPSEnable();
        else mCallBack.onPermissionRequest(false);
    }

    //TODO : 检查GPS是否打开
    public void onCheckGPSEnable() {
        boolean gpsEnable = PermissionUtil.onGetLocationEnabled(mActivity);
        if (gpsEnable) mCallBack.onPermissionRequest(true);
        else mCallBack.onShowGPSTips();
    }

    //TODO : 获取相机权限
    public void onCheckCameraPermission(FragmentActivity activity, OnPermissionCallBack callBack) {
        boolean granted = PermissionUtil.onGetCameraPermission(activity);
        if (granted) callBack.onPermissionRequest(true);
        else {
            PermissionDialog dialog = new PermissionDialog(mActivity, PERMISSION_CAMERA, callBack);
            dialog.show(activity.getSupportFragmentManager(), TAG);
        }
    }

    //TODO : 获取相消息推送权限
    public void onCheckPushPermission(FragmentActivity activity, OnPermissionCallBack callBack) {
        boolean granted = PermissionUtil.isNotificationEnabled(activity);
        if (granted) callBack.onPermissionRequest(true);
        else {
            PermissionDialog dialog = new PermissionDialog(mActivity, PERMISSION_PUSH, callBack);
            dialog.show(activity.getSupportFragmentManager(), TAG);
        }
    }
}
